package org.nlp2rdf.navigator.benchmark;

import org.dllearner.core.EvaluatedDescription;
import org.nlp2rdf.navigator.client.common.ClassificationResultDTO;
import org.nlp2rdf.navigator.client.common.ExampleDTO;
import org.nlp2rdf.navigator.client.exception.NKEConceptParseException;
import org.nlp2rdf.navigator.client.exception.NKEQueryException;
import org.nlp2rdf.navigator.client.exception.NKERuntimeException;
import org.nlp2rdf.navigator.refactored.IClassifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by Claus Stadler
 * Date: Oct 12, 2010
 * Time: 5:49:45 PM
 */
public class NkeGenericClassifier
    implements IGenericClassifier<String, EvaluatedDescription>
{
    private static final Logger logger = LoggerFactory.getLogger(NkeGenericClassifier.class);

    private IClassifier classifier;
    private int offset;
    private int limit;

    public int getOffset() {
        return offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public NkeGenericClassifier(IClassifier classifier)
    {
        this.classifier = classifier;
    }

    @Override
    public Set<String> classify(EvaluatedDescription description)
            throws Exception
    {
        return retrieveExamples(description);
    }

    @Override
    public int countInstances(EvaluatedDescription description)
            throws Exception
    {
        int result = 0;
        result = classifier.classifyCount(description.getDescription().toKBSyntaxString()).getCount();

        return result;
    }

    private Set<String> retrieveExamples(EvaluatedDescription ed)
            throws NKEConceptParseException, NKEQueryException, NKERuntimeException
    {
        ClassificationResultDTO clasret = classifier.classify(ed.getDescription().toKBSyntaxString(), limit, offset);

        Set<String> retrieved = new HashSet<String>();
        for (ExampleDTO string : clasret.getRetrievedExamples()) {
            retrieved.add(string.getUri());
        }

        return retrieved;
    }


}
